package com.fjwt.gz.proxy.ctrl.award;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardAgentDTO;
import com.fjwt.gz.db.dto.AwardAllocationAgentDTO;
import com.fjwt.gz.db.vo.AwardAgentVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardAgentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 奖品分配机构
 */
@RestController
@Slf4j
@RequestMapping("/api/activity/award/agent")
public class AwardAgentController extends CommonCtrl {

    @Autowired
    private AwardAgentService awardAgentService;

    /**
     * 获取分配机构的列表数据
     * @return
     */
    @GetMapping
    public ApiRes list() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
        IPage<AwardAgentVO> page = awardAgentService.getList(getIPage(true), awardAgentDTO);
        return ApiRes.page(page);
    }

    /**
     * 新增奖品分配机构数量
     *
     * @return
     */
    @PostMapping
    public ApiRes add() {
        AwardAllocationAgentDTO dto = getObject(AwardAllocationAgentDTO.class);
        log.info("机构端新增奖品分配机构数量，请求地址：/api/activity/award/agent，请求参数：{}", JSONUtil.toJsonStr(dto));
        awardAgentService.addByBatch(dto);
        return ApiRes.ok();
    }

    /**
     * 修改奖品分配机构数量
     *
     * @return
     */
    @PutMapping
    public ApiRes update() {
        AwardAllocationAgentDTO dto = getObject(AwardAllocationAgentDTO.class);
        log.info("机构端修改奖品分配机构数量，请求地址：/api/activity/award/agent，请求参数：{}", JSONUtil.toJsonStr(dto));
        awardAgentService.updateByBatch(dto);
        return ApiRes.ok();
    }

    /**
     * 导入excel的机构分配数量
     * @return
     */
    @PostMapping("/importExcelToAdd")
    public ApiRes importExcelToAdd() {
        AwardAllocationAgentDTO allocationAgentDTO = getObject(AwardAllocationAgentDTO.class);
        log.info("导入excel的机构分配数量：{}", allocationAgentDTO);
        awardAgentService.importAwardAgentByExcel(allocationAgentDTO, false);
        return ApiRes.ok();
    }

    /**
     * 导入excel追加机构分配数量
     * @return
     */
    @PostMapping("/importExcelToUpdate")
    public ApiRes importExcelToUpdate() {
        AwardAllocationAgentDTO allocationAgentDTO = getObject(AwardAllocationAgentDTO.class);
        awardAgentService.importAwardAgentByExcel(allocationAgentDTO, true);
        return ApiRes.ok();
    }

    /**
     * 添加单个奖品分配机构数量
     * @return
     */
    @PostMapping("/addOne")
    public ApiRes addOne() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
        log.info("机构端添加单个奖品分配机构数量接口：/api/activity/award/agent/addOne，参数：{}", awardAgentDTO);
        awardAgentService.addOrUpdateOneAgentAwardNum(awardAgentDTO);
        return ApiRes.ok();
    }

    /**
     * 追加更新单个奖品分配机构数量
     * @return
     */
    @PostMapping("/updateOne")
    public ApiRes updateOne() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
//        awardAgentService.addOneAgentAwardNum1(awardAgentDTO, true);
        return ApiRes.ok();
    }

    /**
     * 更新渠道码是否可见
     * @return
     */
    @PostMapping("/updateChannelDisplay")
    public ApiRes updateEmpChannelDisplay() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
        awardAgentService.updateEmpChannelDisplay(awardAgentDTO);
        return ApiRes.ok();
    }

    /**
     * 删除单个奖品分配机构数量
     * @return
     */
    @PostMapping("/removeOne")
    public ApiRes removeOne() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
        log.info("机构端删除单个奖品分配机构数量接口：/api/activity/award/agent/removeOne，参数：{}", awardAgentDTO);
        awardAgentService.removeOneAwardAgent(awardAgentDTO);
        return ApiRes.ok();
    }

    /**
     * 删除奖品分配机构数量
     *
     * @return
     */
    @PostMapping("/removeAll")
    public ApiRes remove() {
        AwardAgentDTO awardAgentDTO = getObject(AwardAgentDTO.class);
        log.info("机构端删除奖品分配机构数量，请求地址：/api/activity/award/agent，请求参数：{}", JSONUtil.toJsonStr(awardAgentDTO));
        awardAgentService.removeSavedData(awardAgentDTO);
        return ApiRes.ok();
    }

}